#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QColorDialog>
#include <QFileDialog>
#include <QFontDialog>
#include <QInputDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_action_new_triggered()
{
    QMessageBox::information(this,tr("菜单测试"),tr("你选择了\"文件\"菜单下的\"新建\"菜单命令"));
}

void MainWindow::on_action_copy_triggered()
{
    ui->textEdit->copy();
}


void MainWindow::on_action_paste_triggered()
{
    ui->textEdit->paste();
}


void MainWindow::on_action_cut_triggered()
{
    ui->textEdit->cut();
}


void MainWindow::on_action_undo_triggered()
{
    ui->textEdit->undo();
}


void MainWindow::on_action_color_triggered()
{
    QColor c = QColorDialog::getColor(Qt::black,this,tr("颜色对话框"));
    /*
    QColorDialog dlg(this);
    dlg.setOption(QColorDialog::ShowAlphaChannel);
    dlg.exec();
    QColor c =dlg.currentColor();
    */
    ui->textEdit->setTextColor(c);

}


void MainWindow::on_action_open_triggered()
{
    //QString filter ;
    //QString fileName = QFileDialog::getOpenFileName(this,tr("文件对话框"),"f:/temp","文本文件(*.txt)",&filter);
    //ui->textEdit->setText(filter);
    QString fileName = QFileDialog::getOpenFileName(this,tr("文件对话框"),"f:/temp","文本文件(*.txt)");
    ui->textEdit->setText(fileName);

}


void MainWindow::on_action_font_triggered()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok){
        ui->textEdit->setFont(font);
    }

}


void MainWindow::on_action_add_triggered()
{
    bool ok;
    QString name = QInputDialog::getText(this,tr("输入字符串对话框"),tr("姓名:"),QLineEdit::Normal,tr("Qt"),&ok);

    QStringList items;
    items << tr("男") << tr("女") ;
    QString sex = QInputDialog::getItem(this, tr("输入条目对话框"), tr("性别:"), items, 0, false, &ok);

    int age = QInputDialog::getInt(this,tr("输入整数对话框"),tr("年龄:"),20,1,150,1,&ok);

    double score = QInputDialog::getDouble(this, tr("输入小数对话框"), tr("成绩:"), 80, 0, 120, 1, &ok, Qt::WindowFlags(), 1);

    QString des = QInputDialog::getMultiLineText(this, tr("输入多行文本对话框"), tr("备注:"), "清华大学出版社教材\n第4章的标准输入对话框", &ok);

    ui->textEdit->setText(tr("姓名: %1\n性别: %2\n年龄: %3\n成绩: %4\n备注: %5").arg(name).arg(sex).arg(age).arg(score).arg(des));
    // 设置字体颜色
    QColor textColor = QColorDialog::getColor(Qt::black,this,tr("颜色对话框"));
    QString formattedText = tr("<font color='%1'>姓名: %2</font><br><font color='%3'>性别: %4</font><br><font color='%5'>年龄: %6</font><br><font color='%7'>成绩: %8</font><br><font color='%9'>备注: %10</font>")
                                .arg(textColor.name())
                                .arg(name)
                                .arg(textColor.name())
                                .arg(sex)
                                .arg(textColor.name())
                                .arg(age)
                                .arg(textColor.name())
                                .arg(score)
                                .arg(textColor.name())
                                .arg(des);

    ui->textEdit->setHtml(formattedText);

}

